<?php

namespace App\Service\playlet;
use App\Model\PlayletBlocks;
use App\Model\PlayletBlocksRanking;
use App\Service\CommicService;
use App\Model\Applet;
use Hyperf\Utils\ApplicationContext;
use App\Common\Response\SystemCode;
class AppletService extends CommicService
{
    const cacheDuration = 60 * 15;
    public function __construct()
    {
        $container = ApplicationContext::getContainer();
        $this->redisService = $container->get(\Hyperf\Redis\Redis::class);
    }


    const STATUS_ENABLE  = 1;


    public static $appletType = [
        '1'       => '微信小程序',
        '2'       => '抖音小程序',
        '3'       => '快手小程序',
        '10000'       => '客户使用'
    ];
    public function search(Array $params){

        $keyword     = $params['keyword'] ?? '';
        $status      = $params['status'] ?? '';
        $pageSize    = $params['pageSize'] ?? 10;
        $page        = $params['page'] ?? 1;

        $query = Applet::query()->select(['id','status','name','cover','app_id','coin_name','created_at','updated_at','type']);

        if ($keyword) {
            $query->orWhere([['name', 'like', '%'.$keyword.'%']]);
        }
        if ($status != '') {
            $query->where(['status' => $status]);
        }
        $models = $query->orderBy('id','desc')->paginate($pageSize,['*'],'page',$page);
        $items = $models->items();
        $selectData = [];
        foreach ($items as $item){
            $data = $item->toArray();
            $data['status_name']  = self::$commonStatus[$item->status];
            $data['type_name'] = self::$appletType[$item->type];
            $selectData[] =$data;
        }
        $data = $models->toArray();
        $data['data'] = $selectData;
        return $data;
    }

    public function createOrUpdate(Array $postData,bool $is_update = false,Applet $appletInfo = null){
        $name = $postData['name'];
        $cover = $postData['cover'] ?? '';
        $app_id = $postData['app_id'];
        $app_secret = $postData['app_secret'];
        $token = $postData['token'];
        $user_default_headimg = $postData['user_default_headimg'] ?? '';
        $static_base_url = $postData['static_base_url'] ?? '';
        $coin_name = $postData['coin_name'] ?? '';
        $status = $postData['status'];
        $type = $postData['type'] ?? 1;
        $model = !empty($appletInfo)? $appletInfo : new Applet();
        $is_check_user = true;
        $is_check_appid= true;

        if($is_update) {
            //修改时判断用户名是否一致
            if ($name == $model->name) {
                $is_check_user = false;
            }
            //修改时判断用户名是否一致
            if ($app_id == $model->app_id) {
                $is_check_appid = false;
            }
        }

        if($is_check_user){
            $appletInfo = Applet::query()->where('name',$name)->first();
            if(!empty($appletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序已存在');
            }
            $model->name = $name;
        }

        if($is_check_appid){
            $appletInfo = Applet::query()->where('app_id',$app_id)->first();
            if(!empty($appletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, 'APPID已存在');
            }
            $model->app_id = $app_id;
        }

        $model->app_secret = $app_secret;
        $model->token = $token;
        if(!empty($cover)){
            $model->cover = $cover;
        }
        if(!empty($user_default_headimg)){
            $model->user_default_headimg = $user_default_headimg;
        }
        $model->static_base_url = $static_base_url;
        $model->coin_name = $coin_name;
        $model->status = $status;
        $model->type = $type;
        if($model->save()){
            //判断是否存在
//            $blockInfo = PlayletBlocks::query()->where(['applet_id' => $model->id])->first();
//            if(empty($blockInfo)){
//                $ids = [];
//                //新建ranking
//                $ids[] = $this->insertRank('banner','101');
//                $ids[] = $this->insertRank('导航','102');
//                $ids[] = $this->insertRank('本周精选','103');
//                $ids[] = $this->insertRank('热播排行','104');
//                $ids[] = $this->insertRank('优选好剧','105');
//
//                $insertInfo =  new PlayletBlocks();
//                $insertInfo->name = '小剧场';
//                $insertInfo->applet_id = $model->id;
//                $insertInfo->template_type = 1;
//                $insertInfo->ranking_ids = implode(',',$ids);
//                $insertInfo->cateid = 5;
//                $insertInfo->save();
//            }
            return $this->response(SystemCode::SYSTEM_SUCCESS);
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR,'保存错误');
        }
    }

    public function insertRank($name,$template_type){

       $insertRes =  PlayletBlocksRanking::create(['name' => $name,'template_type' => $template_type]);
       return $insertRes->id;
    }
}